//package org.example.serve;
//
//import ai.onnxruntime.*;
//import org.apache.commons.io.IOUtils;
//import org.example.util.ImagePreprocessor;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//import java.io.InputStream;
//import java.util.Collections;
//
//@Service
//public class OnnxModelService {
//    private static final int MODEL_INPUT_WIDTH = 640;
//    private static final int MODEL_INPUT_HEIGHT = 640;
//    private static final float[] MEAN = {0.485f, 0.456f, 0.406f};
//    private static final float[] STD = {0.229f, 0.224f, 0.225f};
//
//    private OrtEnvironment environment;
//    private OrtSession session;
//    private String modelInputName;
//
//    @Value("${onnx.model.path}")
//    private String modelPath;
//
//    @PostConstruct
//    public void init() throws Exception {
//        environment = OrtEnvironment.getEnvironment();
//        InputStream modelStream = null;
//        try {
//            modelStream = new ClassPathResource(modelPath).getInputStream();
//            byte[] modelBytes = IOUtils.toByteArray(modelStream);
//
//            OrtSession.SessionOptions options = new OrtSession.SessionOptions();
//            session = environment.createSession(modelBytes, options);
//            this.modelInputName = session.getInputNames().iterator().next();
//        } finally {
//            if (modelStream != null) {
//                modelStream.close();
//            }
//        }
//    }
//
//    public boolean containsUVA(byte[] imageBytes) throws Exception {
//        BufferedImage processedImage = ImagePreprocessor.preprocessImage(
//                imageBytes,
//                MODEL_INPUT_WIDTH,
//                MODEL_INPUT_HEIGHT);
//
//        float[][][][] inputData = ImagePreprocessor.convertToModelInput(
//                processedImage,
//                MEAN,
//                STD);
//
//        OnnxTensor tensor = null;
//        try {
//            tensor = OnnxTensor.createTensor(environment, inputData);
//            OrtSession.Result results = session.run(
//                    Collections.singletonMap(modelInputName, tensor));
//
//            // 获取模型输出
//            Object outputObject = results.get(0).getValue();
//            float outputValue;
//            if (outputObject instanceof float[][][][][]) {
//                // 处理五维数组输出
//                float[][][][][] outputArray = (float[][][][][]) outputObject;
//                // 这里假设取第一个元素作为判断依据，你可以根据实际情况调整
//                outputValue = outputArray[0][0][0][0][0];
//            } else if (outputObject instanceof float[][][][]) {
//                outputValue = ((float[][][][]) outputObject)[0][0][0][0];
//            } else if (outputObject instanceof float[]) {
//                outputValue = ((float[]) outputObject)[0];
//            } else {
//                throw new IllegalArgumentException("Unexpected output type: " + outputObject.getClass().getName());
//            }
//
//            return outputValue > 0.5;
//        } finally {
//            if (tensor != null) {
//                tensor.close();
//            }
//        }
//    }
//}